/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity.prize;

import config.CFPrizePools;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import logging.report.UserAction;
import pgentity.EntityContext;
import share.AMFBuilder;

/**
 *
 * @author Salm
 */
public class PrizeServices {
    private PrizeServices() {}
    private static final PrizeServices inst;
    
    public static PrizeServices inst()
    {
        return inst;
    }
    
    static
    {
        inst = new PrizeServices();
    }
    
    public Object awardPrizePools(CFPrizePools prizePools,
            EntityContext context, UserAction act, long now)
    {
        List<Integer> pzIndices = prizePools.randomPrizes();
        Map<String, Object> prizeResps = new HashMap(pzIndices.size());
        for (int i = 0; i < pzIndices.size(); ++i)
        {
            int prizeIndex = pzIndices.get(i);
            PGPrize prize = PrizeFactory
                    .getPrize(prizePools.getPrize(i, prizeIndex).get(), act);
            
            if (!prize.canPrize(context, now))
                continue;

            Object pzDesc = prize.award(context, now);
            prizeResps.put(String.valueOf(i), 
                    AMFBuilder.make("prize", prizeIndex, "desc", pzDesc));
        }
        
        return prizeResps;
    }
}
